package com.travelsmart.client.apresentacao.widget;

import com.google.gwt.animation.client.Animation;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.travelsmart.client.apresentacao.i18n.I18nMessages;


public class FormAdminDialog extends PopupPanel {
	private static final String STYLE_LOADING = "travelsmart-form-loading-text";
	final Label lblWait = new Label(I18nMessages.INSTANCE.carregando());
	private AbsolutePanel absolutePanel = null;
	private Widget animateeBottom = null;
	private Widget animateeLeft = null;
	private Widget animateeRight = null;
	private Widget animateeTop = null;
	private CustomAnimation animation = null;

	public FormAdminDialog(String info) {
		super(true);
		setAnimationEnabled(true);
		//
		absolutePanel = new AbsolutePanel();
		absolutePanel.setSize("200px", "110px");
		absolutePanel.ensureDebugId("cwAbsolutePanel");
		// Add a widget that we will animate
		animateeTop = new HTML(info);
		animateeTop.setStyleName(STYLE_LOADING);
		animateeBottom = new HTML(info);
		animateeBottom.setStyleName(STYLE_LOADING);
		animateeLeft = new HTML(info);
		animateeLeft.setStyleName(STYLE_LOADING);
		animateeRight = new HTML(info);
		animateeRight.setStyleName(STYLE_LOADING);
		absolutePanel.add(animateeTop);
		absolutePanel.add(animateeBottom);
		absolutePanel.add(animateeLeft);
		absolutePanel.add(animateeRight);
		// Create the custom animation
		animation = new CustomAnimation();
		setWidget(absolutePanel);
	}

	public void setLoadingText(String text) {
		lblWait.setText(text);
	}

	public void center() {
		animation.run(60000);
		super.center();
	}

	public void hide() {
		animation.cancel();
		super.hide();

	}

	public class CustomAnimation extends Animation {
		private int centerX = 50;
		private int centerY = 50;
		private int radius = 45;

		@Override
		protected void onUpdate(double progress) {
			double radian = 2 * Math.PI * progress;
			updatePosition(animateeLeft, radian, 0);
			updatePosition(animateeBottom, radian, 0.5 * Math.PI);
			updatePosition(animateeRight, radian, Math.PI);
			updatePosition(animateeTop, radian, 1.5 * Math.PI);
		}

		private void updatePosition(Widget w, double radian, double offset) {
			radian += offset;
			double x = radius * Math.cos(radian) + centerX;
			double y = radius * Math.sin(radian) + centerY;
			absolutePanel.setWidgetPosition(w, (int) x, (int) y);
		}
	}

}
